VFG.MeasureLength = function(viewer) {
	this.viewer = viewer;
	this.positions = [];
	this.poly = null;
	this.distance = 0;
	this.cartesian = null;
	this.floatingPoint = [];
	this.enity;
	this.tip = new VFG.Tip(this.viewer);
	this.init();
}
VFG.MeasureLength.prototype.init = function() {
	let _this = this;
	this.handler = new Cesium.ScreenSpaceEventHandler(_this.viewer.scene.canvas);
	this.handler.setInputAction(function(movement) {
		_this.tip.showAt(movement.endPosition, "单击开始，右击结束");
		_this.cartesian = VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.enity);
		if (_this.positions.length >= 2) {
			if (!_this.poly) {
				_this.poly = _this.viewer.entities.add({
					name: '直线',
					polyline: {
						show: true,
						positions: _this.positions,
						material: new Cesium.PolylineGlowMaterialProperty({
                     		glowPower : 0.5,
                      		color : Cesium.Color.fromCssColorString('#ffff00'),
                 		}),
						//material: Cesium.Color.fromCssColorString('#ffff00'),
						width: 3,
						clampToGround: true
					}
				});
			} else {
				_this.positions.pop();
				_this.positions.push(_this.cartesian);
				_this.poly.polyline.positions = _this.update();
			}
			_this.distance = _this.getSpaceDistance(_this.positions);
			_this.tip.showAt(movement.endPosition, _this.distance + "米");
		}
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

	this.handler.setInputAction(function(movement) {
		_this.cartesian = VFG.Util.getScreenToC3(_this.viewer, movement.position, _this.enity);
		if (_this.positions.length == 0) {
			_this.positions.push(_this.cartesian.clone());
		}
		_this.positions.push(_this.cartesian);
		//在三维场景中添加Label
		_this.addLabel();
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

	this.handler.setInputAction(function(movement) {
		//_this.positions.pop(); //最后一个点无效
		_this.addLabel();
		if(_this.success){
			_this.success();
		}
		_this.stop();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

};

VFG.MeasureLength.prototype.addLabel = function() {
	var _this=this;
	_this.floatingPoint.push(_this.viewer.entities.add({
		name: '空间直线距离',
		position: _this.positions[_this.positions.length - 1],
		point: {
			pixelSize: 5,
			color: Cesium.Color.RED,
			outlineColor: Cesium.Color.WHITE,
			outlineWidth: 2,
		},
		label: {
			text: _this.distance + "米",
			font: '18px sans-serif',
			fillColor: Cesium.Color.GOLD,
			style: Cesium.LabelStyle.FILL_AND_OUTLINE,
			outlineWidth: 2,
			verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
			pixelOffset: new Cesium.Cartesian2(20, -20),
		}
	}));
}

VFG.MeasureLength.prototype.update = function() {
	var _this = this;
	return new Cesium.CallbackProperty(function() {
		return _this.positions;
	}, false);
}

VFG.MeasureLength.prototype.getSpaceDistance = function(positions) {
	var distance = 0;
	for (var i = 0; i < positions.length - 1; i++) {

		var point1cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
		var point2cartographic = Cesium.Cartographic.fromCartesian(positions[i + 1]);
		/**根据经纬度计算出距离**/
		var geodesic = new Cesium.EllipsoidGeodesic();
		geodesic.setEndPoints(point1cartographic, point2cartographic);
		var s = geodesic.surfaceDistance;
		//console.log(Math.sqrt(Math.pow(distance, 2) + Math.pow(endheight, 2)));
		//返回两点之间的距离
		s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
		distance = distance + s;
	}
	return distance.toFixed(2);
}

VFG.MeasureLength.prototype.addListener=function(callback){
	if(callback!=null){
		this.success=callback;
	}
}

VFG.MeasureLength.prototype.removeListener=function(){
	this.success=null;
}

VFG.MeasureLength.prototype.stop = function() {
	if (this.tip) {
		this.tip.destroy();
	}
	if (this.handler) {
		this.handler.destroy();
		this.handler = null;
	};
}

VFG.MeasureLength.prototype.destroy = function() {
	var _this = this;
	if (this.tip) {
		this.tip.destroy();
	}
	if (this.handler) {
		this.handler.destroy();
		this.handler = null;
	};
	if (this.poly) {
		this.viewer.entities.remove(this.poly);
	}
	if (this.floatingPoint && _this.floatingPoint.length>0) {
		for(var i = 0; i < _this.floatingPoint.length; i++) {
			this.viewer.entities.remove(_this.floatingPoint[i]);
		}
	}
	delete this.poly
	delete this.floatingPoint
	delete this.handler
	delete this.viewer
};